const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'
const ctime = require('../../time/conversion')
//获取自己部门的所有员工信息
router.post('/getEmployees', async (req, res) => {
    const result = jwt.verify(req.body.token, SECRET)
    console.log(result.username)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        //验证用户对应角色的权限

        if (response_temp.length != null && response_temp.length > 0) {
            sql = 'select workNumber,name,age,gender,address,education,contact,stationName,salaryBasic,overtimeWage,bonus,deductionWages,currentTask,contractStart,contractEnd from department_employees WHERE departmentId= ?'
            let data = [response_temp[0].departmentId]
            if (req.body.getWorkNumber != null) {
                sql = 'select workNumber,name from department_employees WHERE departmentId = ? and currentTask IS NULL'
                data = [response_temp[0].departmentId]
                db.base(sql, data, response => {
                    console.log(response)
                    console.log("查询无工作的员工")
                    res.json(response)
                })
            }
            //查找指定工号的职员
            else if(req.body.workNumber !== '') {
                sql = 'select workNumber,name,age,gender,address,education,contact,stationName,salaryBasic,overtimeWage,bonus,deductionWages,currentTask,contractStart,contractEnd from department_employees WHERE departmentId= ? and workNumber = ?'
                data = [response_temp[0].departmentId, req.body.workNumber]
                db.base(sql, data, response => {
                    let  data = []
                    for (i = 0; i < response.length; i++) {
                        if (response[i].gender == 1) {
                            response[i].gender = "男"
                        } else {
                            response[i].gender = "女"
                        }
                        //转换时间
                        ctime.formatUTC(response[i].contractStart, result => {
                            response[i].contractStart = result
                        })
                        ctime.formatUTC(response[i].contractEnd, result => {
                            response[i].contractEnd = result
                        })
                        //判断是否存在工作任务
                        if(response[i].currentTask === null) {
                            response[i].currentTask = '无'
                        }
                        data[i] = {
                            'workNumber': response[i].workNumber,
                            'name': response[i].name,
                            'age': response[i].age,
                            'gender': response[i].gender,
                            'address': response[i].address,
                            'education': response[i].education,
                            'contact': response[i].contact,
                            'stationName': response[i].stationName,
                            'salary': response[i].salaryBasic + response[i].overtimeWage + response[i].bonus - response[i].deductionWages,
                            'currentTask': response[i].currentTask,
                            'contractStart': response[i].contractStart,
                            'contractEnd': response[i].contractEnd
                        }
                    }
                    console.log(data)
                    res.json({data})
                })
            }
            else {
                db.base(sql, data, response => {
                    console.log("查询所有员工")
                    console.log(response)
                    let  data = []
                    for (i = 0; i < response.length; i++) {
                        if (response[i].gender == 1) {
                            response[i].gender = "男"
                        } else {
                            response[i].gender = "女"
                        }
                        //转换时间
                        ctime.formatUTC(response[i].contractStart, result => {
                            response[i].contractStart = result
                        })
                        ctime.formatUTC(response[i].contractEnd, result => {
                            response[i].contractEnd = result
                        })
                        //判断是否存在工作任务
                        if(response[i].currentTask === null) {
                            response[i].currentTask = '无'
                        }
                        data[i] = {
                            'workNumber': response[i].workNumber,
                            'name': response[i].name,
                            'age': response[i].age,
                            'gender': response[i].gender,
                            'address': response[i].address,
                            'education': response[i].education,
                            'contact': response[i].contact,
                            'stationName': response[i].stationName,
                            'salary': response[i].salaryBasic + response[i].overtimeWage + response[i].bonus - response[i].deductionWages,
                            'currentTask': response[i].currentTask,
                            'contractStart': response[i].contractStart,
                            'contractEnd': response[i].contractEnd
                        }
                    }

                    console.log(data)
                    //返回所有的薪水选项
                    let sql = 'select salary_id from salary'
                    db.base(sql, null, response => {
                        let salary_options = []
                        // console.log('salary',response)
                        for(i = 0; i < response.length; i++) {
                            salary_options[i] = {
                                'value': response[i].salary_id,
                                'label': response[i].salary_id
                            }
                        }
                        //返回本部门所有的职位选项
                        let sql = 'select station_name,station_id from stationInfo where department_id = ?'
                        db.base(sql, response_temp[0].departmentId, response => {
                            let station_options = []
                            for(i = 0; i < response.length; i++) {
                                station_options[i] = {
                                    'value': response[i].station_id,
                                    'label': response[i].station_name
                                }
                            }
                            // console.log('station',station_options)
                            //获取目标岗位
                            let sql = 'select * from departmentInfo'
                            db.base(sql, null, response => {
                                let des_station_options = []
                                for(let i = 0; i < response.length; i++) {
                                    des_station_options[i] = {
                                        'value': response[i].department_id + '-' + response[i].station_id,
                                        'label': response[i].station_name + '-' + response[i].department_name + '(' + response[i].region + ')'
                                    }
                                }
                                console.log(des_station_options)
                                res.json({
                                    'data': data,
                                    'salary_options': salary_options,
                                    'station_options': station_options,
                                    'des_station_options': des_station_options
                                })
                            })

                        })


                    })

                })
            }
        } else {
            res.json({
                result: "error",
                reason: "无权限"
            })
        }
    })

})

module.exports = router
